*****************************************************
* 0. Data preparation
*****************************************************


* Rename and recode variables (wz_b, bl_b, art_b), check reporting 
tostring bl_b, generate(dn_bl_b) usedisplayformat
tostring art_b, generate(dn_art_b) usedisplayformat
tostring B_TM076, generate(dn_B_TM076) usedisplayformat
destring dn_B_TM076, replace
tostring B_TM077, generate(dn_B_TM077) usedisplayformat
destring dn_B_TM077, replace
tostring HauptET, generate(dn_HauptET) usedisplayformat
tostring ET_FDZ, generate(dn_ET_FDZ) usedisplayformat
generate dn_ive_teilnahme = dn_B_TM077 + dn_B_TM076 
gen dn_teilnahme_all = .
replace dn_teilnahme_all = 1 if B_TM065 != 0 & dn_ive_teilnahme != 0 & B_TM066K != 0
gen dn_teilnahme_mb = .
replace dn_teilnahme_mb = 1 if B_TM065 != 0 & B_TM066K != 0
gen dn_teilnahme_ive = .
replace dn_teilnahme_ive = 1 if dn_ive_teilnahme != 0 & B_TM066K != 0

* Set cross-sectional (plant number) and time dimension (year)	 
isid bnr jahr
bysort bnr jahr: assert _N == 1 
duplicates report bnr jahr
xtset bnr jahr
	 

* Convert sectoral codes where necessary from WZ2003 to WZ2008 using conversion tables from the German Federal Statistical Office (https://www.destatis.de/DE/Methoden/Klassifikationen/Gueter-Wirtschaftsklassifikationen/Downloads/Klassifikationen-wz-2008-umsteiger-xls.xls?__blob=publicationFile)
gen dn_wz_b_2008 = .
replace dn_wz_b_2008 = 3511 if wz_4St == "3511"
replace dn_wz_b_2008 = 3511 if wz_4St == "4011"
tostring dn_wz_b_2008, replace


			 
* Limit data set to electricity generation (main activity, WZ 2008 = "3511") and to coal- or gas-fired plants
keep if dn_wz_b_2008 == "3511" & inlist(dn_ET_FDZ, "1", "3") 	

* Set cross-sectional (plant number) and time dimension (year)	 
isid bnr jahr
bysort bnr jahr: assert _N == 1 
duplicates report bnr jahr
xtset bnr jahr

* Aggregate plant technology to three different groups: hard coal, lignite and natural gas
generate dn_fuel_type_plant = 1 if inlist(dn_HauptET, "1", "2", "3", "4", "5") 
replace dn_fuel_type_plant = 2 if inlist(dn_HauptET, "11", "12", "13", "14", "15", "16", "17")
replace dn_fuel_type_plant = 3 if inlist(dn_HauptET, "31", "32", "33", "34", "35")

* Generate variable to count number of plants
egen count_unique_bnr = group(bnr)
	

	
* -----------------------------------------------------------
*** Cleaning of Missings
	/* 	Investigate how missings and "zeros" are dealt with in 
		the different underlyung data sets*/
* -----------------------------------------------------------

* Check whether installed capacity is always positive. Set it to missing, if installed capacity is zero and plant has not participated in reporting
	foreach var in EF1101U2_all_mean EF1101U3_all_mean EF1101U4_all_mean EF1201U1_mean EF1201U2_mean EF1202U1_mean EF1202U2_mean  {
		replace `var' = . if `var' == 0 & B_TM066K == 0
	}
		

	foreach var in mbe_EF11_mean mbe_EF23_mean mbe_EF24_sum mbe_EF25_sum {
		replace `var' = . if `var' == 0 & B_TM065 == 0 
	}


* Check investment data from IVE Betriebe, check wether zeros are missions or actual zeros:
	foreach var in bi_Code2001 bi_Code2201 bi_Code3001 bi_Code4001 bi_Code4101 bi_Code4801 bi_Code4901 bi_Code5001 {
		replace `var' = . if `var' == 0 & dn_B_TM076 == 0 & B_TM077 == 0
	}
	* Variables bi_Code4101 and bi_Code4901 are only available until 2009 and 2007, respectively -> set to missing
	replace bi_Code4101 = . if jahr > 2009
	replace bi_Code4901 = . if jahr > 2007		

*****************************************************
* 1. Generate and rename key variables
*****************************************************

*** Divide annual sums by number of monthly reportings		
	/* Output - monthly mean: net electricity generation, useful thermal output */
		foreach var in EF2101U1_all_sum EF2101U3_all_sum EF2101U3_TypA_sum EF2101U1_TypA_sum EF2201U2_sum{
		generate double dn_`var'_mean = `var'/mon_anl2_sum
		}
	/* Fuel input - monthly mean */
		generate double dn_EF401U3_sum_mean = EF401U3_sum/mon_eins_sum
	/* Labor input (working hours) - monthly mean */
		generate double dn_mbe_EF24_sum_mean = mbe_EF24_sum/mbe_monate_sum
	/* Adjust labor input to full time equivalents */
		generate double dn_mbe_EF24_sum_vz = mbe_EF24_sum/(mbe_monate_sum*40*4)
	
		
*** Rename variables

	** Labor input: working hours - monthly mean in logs
	generate double l = ln(dn_mbe_EF24_sum_mean)
	
	** Average wage in levels and logs: 
	generate double wage_level = mbe_EF25_sum/mbe_EF24_sum
	generate double wage = ln(mbe_EF25_sum/mbe_EF24_sum)
	generate double wage_sum = ln(mbe_EF25_sum)
	
	** Capital: Bottleneck capacity net in logs 
	generate double k_level = EF1101U3_TypA_mean
	generate double k = ln(EF1101U3_TypA_mean)
	
	** Fuel input: monthly mean in levels and logs
	generate double m_level = dn_EF401U3_sum_mean
	generate double m = ln(dn_EF401U3_sum_mean)
	
	** Investments: Gross total investments in levels and logs
	generate double inv_level = bi_Code4001  
	generate double inv = ln(bi_Code4001)  
	
	** Investments in machinery in levels and in logs (/ logs + 1)	
	generate inv_mach_level = bi_Code3001
	generate inv_mach = ln(bi_Code3001)
	generate inv_mach1 = ln(bi_Code3001+1)
	
	* Investments in machinery per bottleneck capacity
	generate inv_mach_mw = inv_mach_level / EF1101U2_all_mean 
	generate ln_inv_mach_mw = ln(inv_mach_mw)

	* Create large binary investment variable
	generate inv_mach_large = 0 if ln_inv_mach_mw <.
	replace inv_mach_large = 1 if ln_inv_mach_mw > 10 & ln_inv_mach_mw <.
	
	* Output: Net electricity generation and useful thermal output - monthly mean in levels and logs
	generate double y_level = dn_EF2101U1_TypA_sum_mean + dn_EF2101U3_TypA_sum_mean
	generate double y = ln(dn_EF2101U1_TypA_sum_mean + dn_EF2101U3_TypA_sum_mean)  
	
	* Capacity factor for output (relative to available capacity * 8760 hours per year)
	generate double dn_cap_factor_gross = (EF2201U2_sum + EF2101U3_all_sum)/(EF1201U1_mean*8760)
	
	*	Beware of monthly reporting for construction of capacity factor, use monthly means of electricity generation and scale with 730 hours per month
	generate double dn_cf_gr_elec_mon = dn_EF2101U1_TypA_sum_mean/(EF1101U3_TypA_mean*730)
	
	* Plant age (provided by merge with the plant list ("Kraftwerksliste") of the German Federal Network Agency ("Bundesnetzagentur"):
	** Average plant age
	generate double a_mean = jahr - inbetrieb1
	
	** Minimum plant age
	generate double a_min = jahr - inbetrieb2
	
	** Maximum plant age
	generate double a_max = jahr - inbetrieb3
	
	** Adjusted plant age: years since last change
	generate double a_adj = a_min if a_min > 0
	replace a_adj = a_mean if a_min < 0 & a_mean > 0
	replace a_adj = a_max if a_min < 0 & a_mean < 0
	
	* Efficiency rate of output (inverse of heat rate) for conventional plants (gross)
	generate double dn_fuel_efficiency_gross = (EF2101U1_TypA_sum + EF2101U3_TypA_sum + EF601U1_sum + EF601U2_sum)/(EF401U3_sum  * (1/3.6))
	
	* Trim 5% of observations of efficiency rate gross
	pctrim dn_fuel_efficiency_gross, percentiles(2.5 97.5) generate(tr95_) recode(miss)

	* Count efficiency rate larger than one for trimmed sample (95%)
	generate double tr95_eff_1 = 0 if tr95_dn_fuel_efficiency_gross <.
	replace tr95_eff_1 = 1 if tr95_dn_fuel_efficiency_gross > 1 & tr95_dn_fuel_efficiency_gross <.
	
	* Trim 1% of observations of efficiency rate gross
	pctrim dn_fuel_efficiency_gross, percentiles(0.5 99.5) generate(tr99_) recode(miss)
	
	* Count efficiency rate larger than one for trimmed sample (99%)
	generate double tr99_eff_1 = 0 if tr99_dn_fuel_efficiency_gross <.
	replace tr99_eff_1 = 1 if tr99_dn_fuel_efficiency_gross > 1 & tr99_dn_fuel_efficiency_gross <.
	
	
******************************************************************************
* 2. External data on emission factors, annual prices and supply data
****************************************************************************** 
*** a) Merge emission factors for fuels in t CO2 / GJ via variable "HauptET" and calculate CO2 emissions
		/* 	Source: Umweltbundesamt (UBA) / German Federal Environmental Agency
			Umweltbundesamt (2013), Entwicklung der spezifischen Kohlendioxid-Emissionen 
			des deutschen Strommix in den Jahren 1990 bis 2012 
			(Tabelle Anhang 3: Emissionsfaktoren entsprechend ZSE) */

	* Generate new variable for emission factor and add emission factor for fuels defined in variable "HauptET" to the data set
			generate double dn_emission_factor = .
			* 1: 	Hard coal ("Steinkohlen")
				replace dn_emission_factor = 0.0942 if dn_HauptET == "1"
			* 2: 	Hard coal coke ("Steinkohlenkoks")
				replace dn_emission_factor = 0.105 if dn_HauptET == "2"
			* 3: 	Hard coal briquette ("Steinkohlenbriketts")
				replace dn_emission_factor = 0.093 if dn_HauptET == "3"
			* 5: 	Use general hard coal ("Steinkohlen") emission factor for "other hard coal
				replace dn_emission_factor = 0.0942 if dn_HauptET == "5"
			* 11: 	Raw lignite: can be differentiated by some federal state
						replace dn_emission_factor = 0.1096 if dn_HauptET == "11" 
					* Lower Saxony:
						replace dn_emission_factor = 0.099 if dn_HauptET == "11" && dn_bl_b == "3"
					* Hesse:
						replace dn_emission_factor = 0.111 if dn_HauptET == "11" && dn_bl_b == "6"
					* Brandenburg:
						replace dn_emission_factor = 0.113 if dn_HauptET == "11" && dn_bl_b == "12"
					* Saxony:
						replace dn_emission_factor = 0.113 if dn_HauptET == "11" && dn_bl_b == "14"
					* Thuringia:
						replace dn_emission_factor = 0.104 if dn_HauptET == "11" && dn_bl_b == "16"
					* Saxony-Anhalt:
						replace dn_emission_factor = 0.104 if dn_HauptET == "11" && dn_bl_b == "15"
					* Northrhine-Westphalia:
						replace dn_emission_factor = 0.114 if dn_HauptET == "11" && dn_bl_b == "5"
			* 13:	Lignite briquette ("Braunkohlebriketts")
				replace dn_emission_factor = 0.099 if dn_HauptET == "13"
			* 15:	Fluidized bed coal ("Wirbelschichtkohle")
				replace dn_emission_factor = 0.098 if dn_HauptET == "15"
			* 16:	Pulverized and dry coal ("Staub- und Trockenkohle")
				replace dn_emission_factor = 0.098 if dn_HauptET == "16"
			* 17:	General raw lignite ("Rohbraunkohle) emission factor is used for other lignite ("Sonstige Braunkohlen")
				replace dn_emission_factor = 0.1096 if dn_HauptET == "17"
			* 31: 	Natural gas (Erdgas), petroleum gas (Erdoelgas: 0.058)
				replace dn_emission_factor = 0.056 if dn_HauptET == "31"
			* 32: 	Mine gas ("Grubengas")
				replace dn_emission_factor = 0.055 if dn_HauptET == "32"
			* 34: 	Blast-furnace gas ("Hochofengas (Gichtgas)")
				replace dn_emission_factor = 0.105 if dn_HauptET == "34"
			* 35:	Other gases ("sonst. hergestellte Gase")
				replace dn_emission_factor = 0.060 if dn_HauptET == "35"	



*** b) Annual electricity, CO2 and fuel prices are merged by year variable "jahr"

* Volume-weighted average annual wholesale electricity price Phelix Base Day-ahead in EUR/MWh:
		/* 	Source: Own calculations based on European Energy Exchange (EEX) 
			(https://www.eex.com/de/) */
	generate double dn_price_power_base = .
	replace dn_price_power_base = 			cond(jahr==2003, 29.74, ///
											cond(jahr==2004, 28.81, ///
											cond(jahr==2005, 46.98, ///
											cond(jahr==2006, 50.72, ///
											cond(jahr==2007, 38.95, ///
											cond(jahr==2008, 65.90, ///
											cond(jahr==2009, 39.16, ///
											cond(jahr==2010, 44.67, ///
											cond(jahr==2011, 50.91, ///
											cond(jahr==2012, 42.18,.))))))))))

* Volume-weighted average annual wholesale electricity price Phelix Peak Day-ahead in EUR/MWh:
		/* 	Source: Own calculations based on European Energy Exchange (EEX) 
			(https://www.eex.com/de/) */
	generate double dn_price_power_peak = .
	replace dn_price_power_peak = 			cond(jahr==2003, 37.38, ///
											cond(jahr==2004, 34.54, ///
											cond(jahr==2005, 58.06, ///
											cond(jahr==2006, 64.09, ///
											cond(jahr==2007, 50.46, ///
											cond(jahr==2008, 79.89, ///
											cond(jahr==2009, 47.33, ///
											cond(jahr==2010, 51.44, ///
											cond(jahr==2011, 57.16, ///
											cond(jahr==2012, 48.15,.))))))))))

* Volume-weighted average annual wholesale price for European Emissions Allowances (EUA) Day-ahead in EUR/t CO2:
		/* 	Source: Own calculations based on European Energy Exchange (EEX) 
			(https://www.eex.com/de/) */
	generate double dn_price_eua = .
	replace dn_price_eua = 					cond(jahr==2003, 0.00, ///
											cond(jahr==2004, 0.00, ///
											cond(jahr==2005, 21.67, ///
											cond(jahr==2006, 17.10, ///
											cond(jahr==2007, 1.49, ///
											cond(jahr==2008, 22.45, ///
											cond(jahr==2009, 13.50, ///
											cond(jahr==2010, 14.03, ///
											cond(jahr==2011, 11.92, ///
											cond(jahr==2012, 7.21,.))))))))))

* Volume-weighted average annual wholesale future price for European Emissions Allowances (EUA) in EUR/t CO2:
		/* 	Source: Own calculations based on European Energy Exchange (EEX)
			(https://www.eex.com/de/) */
	generate double dn_price_eua_futures = .
	replace dn_price_eua_futures = 			cond(jahr==2003, 0.00, ///
											cond(jahr==2004, 0.00, ///
											cond(jahr==2005, 21.92, ///
											cond(jahr==2006, 18.14, ///
											cond(jahr==2007, 20.89, ///
											cond(jahr==2008, 22.96, ///
											cond(jahr==2009, 13.12, ///
											cond(jahr==2010, 15.32, ///
											cond(jahr==2011, 14.56, ///
											cond(jahr==2012, 7.70,.))))))))))										
											
* Volume-weighted average annual wholesale price for European Emissions Allowances (EUA) Future Prices in t-1 for t in EUR/t CO2:
		/* Except for 2005: take future price in 2005 for expectation 	
		   Source: Own calculations based on European Energy Exchange (EEX)
		   (https://www.eex.com/de/) 
			 */
	generate double dn_price_eua_expected = .
	replace dn_price_eua_expected = 		cond(jahr==2003, 0.00, ///
											cond(jahr==2004, 0.00, ///
											cond(jahr==2005, 21.92, ///
											cond(jahr==2006, 22.08, ///
											cond(jahr==2007, 17.33, ///
											cond(jahr==2008, 20.74, ///
											cond(jahr==2009, 22.81, ///
											cond(jahr==2010, 12.24, ///
											cond(jahr==2011, 15.02, ///
											cond(jahr==2012, 14.75,.))))))))))										
											
											
											
* Annual average cross-border natural gas price in EUR/TJ:
		/* 	Source: Bundesamt fuer Wirtschaft und Ausfuhrkontrolle (BAFA)
			(http://www.bafa.de/bafa/de/energie/erdgas/ausgewaehlte_statistiken/egasmon_xls.xls) */
	generate double dn_price_natural_gas = .
	replace dn_price_natural_gas =			cond(jahr==2003, 3401.00, ///
											cond(jahr==2004, 3288.00, ///
											cond(jahr==2005, 4479.00, ///
											cond(jahr==2006, 5926.00, ///
											cond(jahr==2007, 5550.00, ///
											cond(jahr==2008, 7450.00, ///
											cond(jahr==2009, 5794.00, ///
											cond(jahr==2010, 5726.00, ///
											cond(jahr==2011, 7133.00, ///
											cond(jahr==2012, 8061.00,.))))))))))								
								
* Annual average third-country hard coal price in EUR/ t SKE (hard coal units):
		/* 	Source: Bundesamt fuer Wirtschaft und Ausfuhrkontrolle (BAFA)
			(http://www.bafa.de/bafa/de/energie/steinkohle/drittlandskohlepreis/index.html) */
	generate double dn_price_coal = .
	replace dn_price_coal =					cond(jahr==2003, 39.87, ///
											cond(jahr==2004, 55.36, ///
											cond(jahr==2005, 65.02, ///
											cond(jahr==2006, 61.76, ///
											cond(jahr==2007, 68.24, ///
											cond(jahr==2008, 112.48, ///
											cond(jahr==2009, 78.81, ///
											cond(jahr==2010, 85.33, ///
											cond(jahr==2011, 106.97, ///
											cond(jahr==2012, 93.02,.))))))))))										


********** Calculation of ETS intensity and ETS ex-ante exposure											
											
* Calculate ETS intensity with plant specific heat rate and emission factor in Euro / MWh output (for full and trimmed samples)
generate double ti_eff = (1/dn_fuel_efficiency_gross)*dn_price_eua*(dn_emission_factor*3.6)
generate double ti_eff_exp = (1/dn_fuel_efficiency_gross)*dn_price_eua_expected*(dn_emission_factor*3.6)
	
generate double tr95_ti_eff = (1/tr95_dn_fuel_efficiency_gross)*dn_price_eua*(dn_emission_factor*3.6)
generate double tr95_ti_eff_exp = (1/tr95_dn_fuel_efficiency_gross)*dn_price_eua_expected*(dn_emission_factor*3.6)

generate double tr99_ti_eff = (1/tr99_dn_fuel_efficiency_gross)*dn_price_eua*(dn_emission_factor*3.6)
generate double tr99_ti_eff_exp = (1/tr99_dn_fuel_efficiency_gross)*dn_price_eua_expected*(dn_emission_factor*3.6)
	
* Calculate ETS intensity with plant specific heat rate and emission factor based on CO2 price futures in Euro / MWh output (for full and trimmed samples)
generate double ti_eff_futures = (1/dn_fuel_efficiency_gross)*dn_price_eua_futures*(dn_emission_factor*3.6)
generate double tr95_ti_eff_futures = (1/tr95_dn_fuel_efficiency_gross)*dn_price_eua_futures*(dn_emission_factor*3.6)
generate double tr99_ti_eff_futures = (1/tr99_dn_fuel_efficiency_gross)*dn_price_eua_futures*(dn_emission_factor*3.6)
	
		
*** Generate ETS ex-ante exposure (for full and trimmed samples)
* Full sample
bys bnr: egen double dn_fuel_efficiency_gross_mean = mean(dn_fuel_efficiency_gross) if jahr == 2003 | jahr == 2004  
bys bnr: egen double dn_fuel_eff_gr_0304 = mean(dn_fuel_efficiency_gross_mean)
	
generate double z = (dn_emission_factor*3.6)*dn_price_eua*(1/dn_fuel_eff_gr_0304)
generate double z_exp = (dn_emission_factor*3.6)*dn_price_eua_expected*(1/dn_fuel_eff_gr_0304)
generate double z_fut = (dn_emission_factor*3.6)*dn_price_eua_futures*(1/dn_fuel_eff_gr_0304)
	
* TR95	
bys bnr: egen double tr95_dn_fuel_eff_gr_mean = mean(tr95_dn_fuel_efficiency_gross) if jahr == 2003 | jahr == 2004  
bys bnr: egen double tr95_dn_fuel_eff_gr_0304 = mean(tr95_dn_fuel_eff_gr_mean)
		
generate double tr95_z = (dn_emission_factor*3.6)*dn_price_eua*(1/tr95_dn_fuel_eff_gr_0304)
generate double tr95_z_exp = (dn_emission_factor*3.6)*dn_price_eua_expected*(1/tr95_dn_fuel_eff_gr_0304)
generate double tr95_z_fut = (dn_emission_factor*3.6)*dn_price_eua_futures*(1/tr95_dn_fuel_eff_gr_0304)
	
* TR99	
bys bnr: egen double tr99_dn_fuel_eff_gr_mean = mean(tr99_dn_fuel_efficiency_gross) if jahr == 2003 | jahr == 2004  
bys bnr: egen double tr99_dn_fuel_eff_gr_0304 = mean(tr99_dn_fuel_eff_gr_mean)
	
generate double tr99_z = (dn_emission_factor*3.6)*dn_price_eua*(1/tr99_dn_fuel_eff_gr_0304)
generate double tr99_z_exp = (dn_emission_factor*3.6)*dn_price_eua_expected*(1/tr99_dn_fuel_eff_gr_0304)
generate double tr99_z_fut = (dn_emission_factor*3.6)*dn_price_eua_futures*(1/tr99_dn_fuel_eff_gr_0304)

		
* Generate fuel price intensity 
generate double fuel_price = .
replace fuel_price = dn_price_coal/8.141 if inlist(dn_HauptET, "1", "2", "3", "4", "5") 
* Lignite price based on BMWI (2014, p.419): 0.4 EUR / GJ
replace fuel_price = 0.4 * 3.6 if inlist(dn_HauptET, "11", "12", "13", "14", "15", "16", "17")
replace fuel_price = dn_price_natural_gas * 0.0036 if inlist(dn_HauptET, "31", "32", "33", "34", "35")

*** Fuel price intensity based on plant-specific heat rate
* Full sample
generate double fp_eff = fuel_price*(1/dn_fuel_efficiency_gross)

* TR95
generate double tr95_fp_eff = fuel_price*(1/tr95_dn_fuel_efficiency_gross)

* TR99
generate double tr99_fp_eff = fuel_price*(1/tr99_dn_fuel_efficiency_gross)


* Generate fuel price ex-ante exposure
generate fp_eff_z = fuel_price*(1/dn_fuel_eff_gr_0304)
generate tr95_fp_eff_z = fuel_price*(1/tr95_dn_fuel_eff_gr_0304)
generate tr99_fp_eff_z = fuel_price*(1/tr99_dn_fuel_eff_gr_0304)

		
*** c) 	Merge annual electricity generation based on year variable "jahr"


	* Annual electricity generation by renewable energy technologies - total in GWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_res_generation_total = .
	replace dn_res_generation_total =		cond(jahr==2003, 45589.00, ///
											cond(jahr==2004, 56632.20, ///
											cond(jahr==2005, 62503.20, ///
											cond(jahr==2006, 71638.40, ///
											cond(jahr==2007, 88321.40, ///
											cond(jahr==2008, 93247.00, ///
											cond(jahr==2009, 94859.00, ///
											cond(jahr==2010, 104810.00, ///
											cond(jahr==2011, 123775.00, ///
											cond(jahr==2012, 143799.00,.))))))))))											
																				
	* Annual electricity generation by renewable energy technologies - wind in GWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_res_generation_wind = .
	replace dn_res_generation_wind =		cond(jahr==2003, 14593.00, ///
											cond(jahr==2004, 16612.00, ///
											cond(jahr==2005, 18375.00, ///
											cond(jahr==2006, 20568.00, ///
											cond(jahr==2007, 22183.00, ///
											cond(jahr==2008, 23815.00, ///
											cond(jahr==2009, 25692.00, ///
											cond(jahr==2010, 27180.00, ///
											cond(jahr==2011, 29060.00, ///
											cond(jahr==2012, 31304.00,.))))))))))	
											
	* Annual electricity generation by renewable energy technologies - solar PV in GWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_res_generation_pv = .
	replace dn_res_generation_pv =			cond(jahr==2003, 313.00, ///
											cond(jahr==2004, 557.00, ///
											cond(jahr==2005, 1282.00, ///
											cond(jahr==2006, 2220.00, ///
											cond(jahr==2007, 3075.00, ///
											cond(jahr==2008, 4420.00, ///
											cond(jahr==2009, 6583.00, ///
											cond(jahr==2010, 11729.00, ///
											cond(jahr==2011, 19599.00, ///
											cond(jahr==2012, 26380.00,.))))))))))											

	* Annual electricity generation by renewable energy technologies - biomass in GWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_res_generation_biomass = .
	replace dn_res_generation_biomass =		cond(jahr==2003, 6603.00, ///
											cond(jahr==2004, 8218.00, ///
											cond(jahr==2005, 11102.00, ///
											cond(jahr==2006, 14793.00, ///
											cond(jahr==2007, 19832.00, ///
											cond(jahr==2008, 23121.00, ///
											cond(jahr==2009, 26255.00, ///
											cond(jahr==2010, 29561.00, ///
											cond(jahr==2011, 32848.00, ///
											cond(jahr==2012, 39682.00,.))))))))))	
											

											
										
*** d) Merge annual electricity consumption

	* Annual domestic gross electricity consumption in TWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_gross_national_consumption = .
	replace dn_gross_national_consumption =	cond(jahr==2003, 600.710, ///
											cond(jahr==2004, 610.159, ///
											cond(jahr==2005, 614.100, ///
											cond(jahr==2006, 619.796, ///
											cond(jahr==2007, 621.463, ///
											cond(jahr==2008, 618.236, ///
											cond(jahr==2009, 581.292, ///
											cond(jahr==2010, 615.386, ///
											cond(jahr==2011, 606.799, ///
											cond(jahr==2012, 607.061,.))))))))))											

	* Annual domestic balance of gross electricity exports in TWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_gross_export_balance = .
	replace dn_gross_export_balance =		cond(jahr==2003, 8.070, ///
											cond(jahr==2004, 7.306, ///
											cond(jahr==2005, 8.475, ///
											cond(jahr==2006, 19.771, ///
											cond(jahr==2007, 19.115, ///
											cond(jahr==2008, 22.450, ///
											cond(jahr==2009, 14.332, ///
											cond(jahr==2010, 17.707, ///
											cond(jahr==2011, 6.271, ///
											cond(jahr==2012, 23.089,.))))))))))											

	* Total electricity demand is calculated as sum of domestic electricity consumption and export balance
	generate double dn_gross_total_demand = dn_gross_national_consumption + dn_gross_export_balance 


	* Annual residual demand for fossil fuels in TWh
		/* 	Source: Bundesministerium fuer Wirtschaft und Energie (BMWi) - Energiedaten
			(http://www.bmwi.de/DE/Themen/Energie/Energiedaten-und-analysen/energiedaten.html) */
	generate double dn_gross_fossil_residual_demand = .
	replace dn_gross_fossil_residual_demand =	cond(jahr==2003, 377.825, ///
												cond(jahr==2004, 372.540, ///
												cond(jahr==2005, 372.885, ///
												cond(jahr==2006, 375.171, ///
												cond(jahr==2007, 385.165, ///
												cond(jahr==2008, 373.950, ///
												cond(jahr==2009, 344.400, ///
												cond(jahr==2010, 360.927, ///
												cond(jahr==2011, 355.755, ///
												cond(jahr==2012, 361.201,.))))))))))	


									
	* Compare electricity generation by fossil fuel power plants (gross) in sample to annual residual demand for fossil fuels (gross)
	bys jahr: egen dn_generation_sample_conv = total(EF2201U2)
	bys jahr: gen da_share_generation_conv = dn_generation_sample_conv/(dn_gross_fossil_residual_demand*1000000)
	
	* Compare electricity generation in sample for plants with information on age (merge with BNetzA data)
	bys jahr: egen dn_generation_sample_conv_match = total(EF2201U2) if a_adj <.
	bys jahr: gen da_share_generation_conv_match = dn_generation_sample_conv_match/(dn_gross_fossil_residual_demand*1000000) if a_adj <.

	generate double dn_power_net_share = EF2101U1_all_sum/(dn_gross_total_demand*1000000)
				

* Calculate output of competitor
	bys jahr: egen double dn_generation_jahr = sum(EF2201U3_sum+EF2201U5_sum)
	generate double dn_generation_competitor = dn_generation_jahr - (EF2201U3_sum+EF2201U5_sum)
	generate double dn_y_competitor = log(dn_generation_competitor)
	
* Calculate output of competitor (only those that have a higher capacity factor)
	gsort +jahr -dn_cf_gr_elec_mon
	by jahr: generate double dn_generation_competitor_mo = sum(EF2201U3_sum+EF2201U5_sum)-(EF2201U3_sum+EF2201U5_sum)
	generate double dn_y_competitor_mo = log(dn_generation_competitor_mo)
	
		
* Calculate output of competitor, including renewables
	generate double dn_generation_competitor_ee = dn_generation_jahr - (EF2201U3_sum+EF2201U5_sum) + dn_res_generation_total
	generate double dn_y_competitor_ee = log(dn_generation_competitor_ee)
		
* Calculate output of competitor only those that have a higher capacity factor), including renewables
	gsort +jahr -dn_cf_gr_elec_mon
	by jahr: generate double dn_generation_competitor_mo_ee = sum(EF2201U3_sum+EF2201U5_sum)-(EF2201U3_sum+EF2201U5_sum) + dn_res_generation_total
	generate double dn_y_competitor_mo_ee = log(dn_generation_competitor_mo_ee)
		
		
* Define size classes based on bottleneck gross electric capacity: below 10 MW, 10 - 80 MW, 80 - 400 MW, larger than or eqal to 400 MW 
	generate double dn_size_class = 0
	replace dn_size_class = 				cond(EF1101U2_TypA_mean<10 , 0, ///
											cond(EF1101U2_TypA_mean>=10&EF1101U2_TypA_mean<80, 1, ///
											cond(EF1101U2_TypA_mean>=80&EF1101U2_TypA_mean<400, 2, ///
											cond(EF1101U2_TypA_mean>=400 & EF1101U2_TypA_mean <., 3, .))))
											
* Define age classes based on adjusted age (years since last change): below 10 years, 10 - 20 years, 20 - 40 years, larger than or eqal to 40 years 
	generate double dn_age_class = 0
	replace dn_age_class = 					cond(a_adj<10, 0, ///
											cond(a_adj>=10&a_adj<20, 1, ///
											cond(a_adj>=20&a_adj<40, 2, ///
											cond(a_adj>=40& a_adj<. , 3, .))))

									
	
		
* Generate interactions with ETS TI and ETS EAE with size class, age class and fuel types
	foreach var of varlist z tr95_z tr99_z z_exp tr95_z_exp tr99_z_exp ti_eff tr95_ti_eff tr99_ti_eff ti_eff_exp tr95_ti_eff_exp tr99_ti_eff_exp{
		forvalues i = 1(1)3{
			generate double `var'_size_`i' = 0
			replace `var'_size_`i' = `var' if dn_size_class == `i'
			}
	}

	foreach var of varlist z tr95_z tr99_z z_exp tr95_z_exp tr99_z_exp ti_eff tr95_ti_eff tr99_ti_eff ti_eff_exp tr95_ti_eff_exp tr99_ti_eff_exp{
		forvalues i = 1(1)3{
			generate double `var'_age_`i' = 0
			replace `var'_age_`i' = `var' if dn_age_class == `i'
			}
	}

	foreach var of varlist z tr95_z tr99_z z_exp tr95_z_exp tr99_z_exp ti_eff tr95_ti_eff tr99_ti_eff ti_eff_exp tr95_ti_eff_exp tr99_ti_eff_exp{
		forvalues i = 2(1)3{
			generate double `var'_fuel_`i' = 0
			replace `var'_fuel_`i' = `var' if dn_fuel_type_plant == `i'
			}
	}


* Show share of electricity generation of sample for fuel input regression vs. labor input regression in whole sample
	bys jahr: egen dn_generation_sample_conv_m = total(EF2201U2) if m <.
	generate double da_generation_sample_conv_m = dn_generation_sample_conv_m / dn_generation_sample_conv
	
	bys jahr: egen dn_generation_sample_conv_l = total(EF2201U2) if l <. & k <. & wage <.
	generate double da_generation_sample_conv_l = dn_generation_sample_conv_l / dn_generation_sample_conv
	
	
* Generate a variable indicating the number of years that a plant reports
	bys bnr: generate nyear=[_N]
	
describe
											
exit	
